#!/bin/bash
. /etc/sipman.conf
eval `$SIM_PROCCGI $*`
# -- Script begins here --
LIST=$1
ACTION=$2
FILE=$3
SRCSTRING=$4
NUMLINES=5000
#echo "$1, $2, $3, $4."
if [ "$ACTION" = "none" ]; then
  ACTION=list
fi
if [ "$LIST" = "none" ]; then
  LIST=dev-current
fi

remFile ()
{
 local FILEDIR=$1
 local FILE=$2
 if [ -d $FILEDIR ] && [ -w "$FILEDIR/$FILE" ]; then
   echo "<PRE>"
   echo "</PRE>"
   rm $FILEDIR/$FILE
   if [ ! -f "$FILEDIR/$FILE" ]; then
     echo "$FILE removed successfully."
   else
     echo "Oops, could not remove file $FILE."
   fi
 fi
}

listDir ()
{
  local FILEDIR=$1
#  if [ "$ACTION" = "list" ] || [ "$ACTION" = "list-search" ]; then
  if [ "$FILEDIR" != "" ]; then
#   echo "Listing directory: $FILEDIR."
    echo "
	<FORM   method=post Action=$SCRIPT_NAME?file+$LIST+search2+all>
	  <INPUT NAME=searchstring1 Size=30 Type=text> and
	  <INPUT NAME=searchstring2 Size=30 Type=text>
	  <INPUT TYPE=submit Value=Search>
	</FORM>
	<TABLE>
	  <TR><TD>Num</TD><TD><A HREF=$SCRIPT_NAME?file+$LIST+list+name>Filename</A></TD>
	  <TD><A HREF=$SCRIPT_NAME?file+$LIST+list+size>Size</A></TD>
	  <TD><A HREF=$SCRIPT_NAME?file+$LIST+list+date>Date</A></TD></TR>"
    NUM=1
    ls $SORTOPTION $FILEDIR | while read V1 V2 V3 V4 SIZE D1 D2 D3 FILENAME REST
    do
#     Laat alleen de leesbare files zien.
      if [ "$FILENAME" = "index.html" ] || [ "$FILENAME" = "" ]; then
	SKIP=yes
      elif [ -r "$FILEDIR/$FILENAME" ] && [ -f "$FILEDIR/$FILENAME" ]; then
	# show files only when they are readable en gt 0 bytes.
	echo "<TR VALIGN=top><TD ALIGN=right>$NUM</TD><TD><A HREF=$SCRIPT_NAME?file+$LIST+read+$FILENAME>$FILENAME</A></TD><TD ALIGN=right>$SIZE</TD>
	      <TD>($D2 $D1 $D3)</TD>"
	if [ "$ACTION" = "list-search" ]; then
	  echo "<TD><FORM   method=post Action=$SCRIPT_NAME?file+search+$LIST+$FILENAME>
		  <INPUT NAME=searchstring Size=40 Type=text>
		  <INPUT TYPE=submit Value=Search>
		</FORM></TD>"
	else
	  if [ "$CMP" = "yes" ]; then
	    if [ "$SRCSTRING" = "" ]; then
	      echo "<TD><A HREF=$SCRIPT_NAME?file+$LIST+list+$FILE+$FILENAME>cmp</A></TD>"
	    else
	      echo "<TD><A HREF=$SCRIPT_NAME?file+$LIST+compare+$SRCSTRING+$FILENAME>cmp</A></TD>"
	    fi
	  fi
	fi
	if [ -w "$FILEDIR/$FILENAME" ]; then
	  echo "<TD><A HREF=$SCRIPT_NAME?file+rm+$LIST+$FILENAME>
		   <IMG SRC=/sipman3/gifs/database_delete.png ALT=\"Delete file.\" BORDER=0>
		</A>"
        fi
        echo "</TR>"
        NUM=`expr $NUM + 1`
      fi
    done
    echo "</TABLE>"
  else
    echo "Sorry your directory was not coded....."
  fi
}

# variabele die bepaald welke directory gelist wordt.
case $LIST in
  (sipman-log)      NAME="Logging directory for Sipman"
                    FILEDIR=$SIM_LOGDIR
                    ;;

  (syslog)          NAME="Syslog directory"
                    #FILEDIR=/var/log
                    FILEDIR=$SIM_SYSLOGSYS
                    ;;

  (apache-log)      NAME="Apache webserver logdir"
                    FILEDIR=$SIM_HTTPLOGDIR
                    ;;

  (main-configs)    NAME="Sipman main config directory"
  		    FILEDIR=$SIM_MAINCONFD
		    ;;

  (data-configs)    NAME="Sipman domain config directory"
                    FILEDIR=$SIM_CONFD
                    ;;

  (agentconfigs)    NAME="Sipman agent config directory"
                    FILEDIR=$SIM_DHCPCONFD
                    ;;
  (zones)	    NAME="DNS zone file directory"
                    FILEDIR=$SIM_NAMEDWORKD
                    ;;
  (shared-configs)   NAME="Sipman shared configs"
		    FILEDIR=$SIM_SHAREDCONFD
		    ;;
  (dev-current)	    NAME="Current device configs"
                    FILEDIR=$SIM_CONFIGD/current
                    CMP=yes
                    ;;
  (dev-history)     NAME="History device configs"
                    FILEDIR=$SIM_CONFIGD/history
                    CMP=yes
                    ;;
  (dev-logs)        NAME="Logging device configs"
                    FILEDIR=$SIM_CONFIGD/logs
                    ;;
  (menu)	    NAME="File menu";;
  (*)               echo " $LIST "
                    FILEDIR="" ;;
esac

# variable die de sorteervolgorde bepaald
case $FILE in
  (date)            SORTOPTION="-lt" ;;
  (size)            SORTOPTION="-lS" ;;
  (*)               SORTOPTION="-l" ;;
esac
echo "<FONT COLOR=#666666>"
#echo "$SIM_PRINTMENU file line file $ACTION $FILE $LIST"
$SIM_PRINTMENU file line file $LIST $ACTION $FILE 
echo "</FONT></TD></TR></TABLE>
      <CENTER>
      <H1>$NAME</H1><HR>"

if [ "$ACTION" = "menu" ]; then
  echo "<TABLE>"
  $SIM_PRINTMENU file table file $LIST $ACTION $FILE 
  echo "</TABLE>"
elif [ "$ACTION" = "list" ] || [ "$ACTION" = "list-search" ]; then
  listDir $FILEDIR
elif [ "$ACTION" = "rm" ]; then
  remFile $FILEDIR $FILE
  listDir $FILEDIR
elif [ "$ACTION" = "search2" ] && [ "$FORM_searchstring1" != "" ]; then
  if [ -d $FILEDIR ]; then
    cd $FILEDIR
    IFS=":"
    if [ "$FORM_searchstring2" = "" ]; then
      echo "<H3>Searching for \"$FORM_searchstring1\" in $FILEDIR</H3></CENTER><PRE>"
      grep -i "$FORM_searchstring1" * | while read FILE REST
      do
        echo "<A HREF=$SCRIPT_NAME?file+$LIST+read+$FILE>$FILE</A>: $REST"
      done
    else
      echo "<H3>Searching for \"$FORM_searchstring1\" and \"$FORM_searchstring2\" in $FILEDIR</H3></CENTER><PRE>"
      grep -i "$FORM_searchstring1" * | grep -i "$FORM_searchstring2" | while read FILE REST
      do
        echo "<A HREF=$SCRIPT_NAME?file+$LIST+read+$FILE>$FILE</A>: $REST"
      done

    fi
    echo "</PRE>"
  fi
elif [ "$ACTION" = "search" ] && [ "$FORM_searchstring" != "" ]; then
  if [ "$FILE" = "all" ]; then
    SRCSTRING=$FORM_searchstring
    NUMFILES=200
    FILE=""
  elif [ "$FORM_searchstring" != "" ]; then
    SRCSTRING=$FORM_searchstring
    NUMFILES=45
  else
    NUMFILES=5
  fi
  if [ "$SRCSTRING" != "" ]; then
    if [ -d $FILEDIR ]; then
      cd $FILEDIR
      echo "<H3>Searching for $SRCSTRING in $FILE ($NUMFILES files)</H3></CENTER><PRE>"
      ls -tr $FILE* | tail -n $NUMFILES | while read SFILE
      do
        if [ -r $SFILE ]; then
          COUNT=1
          echo "<B>Searchresults for $SFILE</B>"
          NEWSTRING=`echo $SRCSTRING | sed -e 's/\./\\\./g'`
#         echo "$NEWSTRING"
#         grep -i "$SRCSTRING" $SFILE | while read LINE
          grep -i "$NEWSTRING" $SFILE | while read LINE
          do
            if [ "$COUNT" -lt "$NUMLINES" ]; then
              if [ "$LINE" != "" ]; then
                echo "($COUNT: $LINE)"
              fi
            elif [ "$COUNT" -eq "$NUMLINES" ]; then
              echo "<BR>
                    Will display no more then $NUMLINES lines per file to keep your browser from crashing....
                    please refine your search..."
#             break
#             exit
            fi
            COUNT=`expr $COUNT + 1`
          done
        else
          echo "Could not read file $SFILE."
        fi
      done
    else
      echo "Oops, could not find your directory...."
    fi
    echo "</PRE><CENTER>"
  else
    echo "No searchstring found!!!"
  fi
elif [ "$ACTION" = "read" ]; then
  NUMLINES=5000  
  echo "<FONT SIZE=+2><B>$FILE</B></FONT>(last $NUMLINES lines.)
        <A HREF=$SCRIPT_NAME?file+$LIST+readall+$FILE>all</A></CENTER><PRE>"
  TYPE=`file $FILEDIR/$FILE`
  MAINTYPE=`echo $TYPE | awk '{ print $2 }'`
  SUBTYPE=`echo $TYPE | awk '{ print $3 }'`
  if [ "${SUBTYPE:0:4}" = "text" ] || [ "$MAINTYPE" = "ASCII" ]; then
    tail -n $NUMLINES $FILEDIR/$FILE
  elif [ "$MAINTYPE" = "gzip" ]; then
    gunzip -c $FILEDIR/$FILE | tail -n $NUMLINES
  else
    echo "Info for $TYPE not supported."
  fi
  echo "</PRE>"
elif [ "$ACTION" = "readall" ]; then
  TYPE=`file $FILEDIR/$FILE`
  MAINTYPE=`echo $TYPE | awk '{ print $2 }'`
  SUBTYPE=`echo $TYPE | awk '{ print $3 }'`
  echo "<FONT SIZE=+2><B>$FILE</B></FONT>
        </CENTER><PRE>"
  if [ "${SUBTYPE:0:4}" = "text" ]; then
    cat $FILEDIR/$FILE
  elif [ "$MAINTYPE" = "gzip" ]; then
    gunzip -c $FILEDIR/$FILE
  else
    echo "Info for $TYPE not supported."
  fi
  echo "</PRE><CENTER>"
elif [ "$ACTION" = "compare" ]; then
  echo "Develloping....
        FILEDIR=$FILEDIR, FILE=$FILE, FILE2=$SRCSTRING
       </CENTER><PRE>"
  diff -C 5 $FILEDIR/$FILE $FILEDIR/$SRCSTRING
  echo "</PRE>"
else
  echo "Oops, please consult your guru....joking...."
fi

